package com.sap.sap_mangers.service.Impl;

import com.sap.sap_mangers.dto.luckyDraw.Price;
import com.sap.sap_mangers.email.IMailService;
import com.sap.sap_mangers.mapper.OtherMapper;
import com.sap.sap_mangers.pojo.SapLuckyDrawTheme;
import com.sap.sap_mangers.pojo.SapLuckyDrawThemePrice;
import com.sap.sap_mangers.pojo.SapLuckyDrawUser;
import com.sap.sap_mangers.pojo.SapOtherCopywriting;
import com.sap.sap_mangers.service.CacheService;
import com.sap.sap_mangers.service.OtherService;
import com.sap.sap_mangers.service.UserService;
import com.sap.sap_mangers.until.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Random;

/**
 * @author 阿东
 * @date 2022/8/30 [12:41]
 */
@Service
public class OtherServiceImpl implements OtherService {
    @Autowired
    private OtherMapper otherMapper;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private CosUntil cosUntil;

    @Autowired
    IMailService iMailService;

    @Autowired
    UserService userService;

    @Override
    public SapOtherCopywriting getPyqCopyWriting() {
        List<SapOtherCopywriting> pyqCopyWriting = otherMapper.getPyqCopyWriting();
        return getSapOtherForLength(pyqCopyWriting);
    }

    /**
     * 随机获取一条长度(27以内)的内容
     *
     * @param writing 各种内容
     * @return 内容
     */
    private static SapOtherCopywriting getSapOtherForLength(List<SapOtherCopywriting> writing) {
        Random random = new Random();
        int index = 0;
        while (true) {
            SapOtherCopywriting sapOtherCopywriting = writing.get(random.nextInt(writing.size()));
            if (sapOtherCopywriting.getOtherCopywritingContext().length() <= 27) {
                return sapOtherCopywriting;
            }
            index++;
            if (index >= 12) {
                return sapOtherCopywriting;
            }
        }
    }

    @Override
    public SapOtherCopywriting getDytcCopyWriting() {
        List<SapOtherCopywriting> dytcCopyWriting = otherMapper.getDytcCopyWriting();
        return getSapOtherForLength(dytcCopyWriting);
    }

    @Override
    public SapOtherCopywriting getWyyCopyWriting() {
        List<SapOtherCopywriting> wyyCopyWriting = otherMapper.getWyyCopyWriting();
        return getSapOtherForLength(wyyCopyWriting);
    }

    @Override
    public SapOtherCopywriting getZaxyCopyWriting() {
        List<SapOtherCopywriting> zaxyCopyWriting = otherMapper.getZaxyCopyWriting();
        return getSapOtherForLength(zaxyCopyWriting);
    }

    @Override
    public SapOtherCopywriting getWaxyCopyWriting() {
        List<SapOtherCopywriting> waxyCopyWriting = otherMapper.getWaxyCopyWriting();
        return getSapOtherForLength(waxyCopyWriting);
    }

    @Override
    public SapOtherCopywriting getDjtCopyWriting() {
        List<SapOtherCopywriting> djtCopyWriting = otherMapper.getDjtCopyWriting();
        return getSapOtherForLength(djtCopyWriting);
    }

    @Override
    public List<SapOtherCopywriting> getPyqCopyWritingSum() {
        return otherMapper.getPyqCopyWriting();
    }

    @Override
    public List<SapOtherCopywriting> getDytcCopyWritingSum() {
        return otherMapper.getDytcCopyWriting();
    }

    @Override
    public List<SapOtherCopywriting> getWyyCopyWritingSum() {
        return otherMapper.getWyyCopyWriting();
    }

    @Override
    public List<SapOtherCopywriting> getZaxyCopyWritingSum() {
        return otherMapper.getZaxyCopyWriting();
    }

    @Override
    public List<SapOtherCopywriting> getWaxyCopyWritingSum() {
        return otherMapper.getWaxyCopyWriting();
    }

    @Override
    public List<SapOtherCopywriting> getDjtCopyWritingSum() {
        return otherMapper.getDjtCopyWriting();
    }

    @Override
    public void createLuckyDrawTheme(String context) {
        otherMapper.createLuckyDrawTheme(context);
    }

    @Override
    public SimpleJson createLuckyDrawPrice(Price price) {
        String imageUrl;
        try {
            imageUrl = cosUntil.getCosUrl(price.getImages().get(0), "SapLuckyDraw/" + otherMapper.getLuckyDrawThemeLast() + "/" + price.getImages().get(0).getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
            return new SimpleJson(1, "图片cos出错");
        }
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(0, 0);
        otherMapper.createLuckyDrawPrice(otherMapper.getLuckyDrawThemeLast(), price.getPrice(), price.getNums(), imageUrl, snowflakeIdWorker.nextId());
        return new SimpleJson(0, "添加奖品成功");
    }

    @Override
    public List<SapLuckyDrawThemePrice> getLuckDrawPriceLastById() {
        List<SapLuckyDrawThemePrice> luckDrawPriceLastById = otherMapper.getLuckDrawPriceLastById(otherMapper.getLuckyDrawThemeLast());
        int num;
        num = luckDrawPriceLastById.size() - 9;
        if (num < 0) {
            int abs = Math.abs(num);
            for (int i = 0; i < abs; i++) {
                SapLuckyDrawThemePrice sapLuckyDrawThemePrice = new SapLuckyDrawThemePrice();
                sapLuckyDrawThemePrice.setLuckyDrawThemeId(otherMapper.getLuckyDrawThemeLast());
                sapLuckyDrawThemePrice.setLuckyDrawThemePrice("暂未添加");
                sapLuckyDrawThemePrice.setLuckyDrawThemePriceImage("https://sapfigure-1-1256773093.cos.ap-nanjing.myqcloud.com//adong奖励.png");
                sapLuckyDrawThemePrice.setLuckyDrawThemePriceNums(0);
                SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(0, 0);
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                sapLuckyDrawThemePrice.setLuckyDrawThemePriceId(snowflakeIdWorker.nextId());
                luckDrawPriceLastById.add(sapLuckyDrawThemePrice);
            }
        }
        return luckDrawPriceLastById;
    }

    @Override
    public List<SapLuckyDrawThemePrice> getLuckDrawPriceLastByIdManager() {
        List<SapLuckyDrawThemePrice> luckDrawPriceLastById = otherMapper.getLuckDrawPriceLastById(otherMapper.getLuckyDrawThemeLast());
        luckDrawPriceLastById.removeIf(sapLuckyDrawThemePrice -> sapLuckyDrawThemePrice.getLuckyDrawThemePriceNums() <= 0);
        for (SapLuckyDrawThemePrice sapLuckyDrawThemePrice : luckDrawPriceLastById) {
            sapLuckyDrawThemePrice.setId(String.valueOf(sapLuckyDrawThemePrice.getLuckyDrawThemePriceId()));
        }
        return luckDrawPriceLastById;
    }

    @Override
    public void joinLuckyDrawByUsername() {
        SapLuckyDrawUser sapLuckyDrawUser = new SapLuckyDrawUser();
        sapLuckyDrawUser.setLuckyDrawUsername(JwtUtil.getUserForToken());
        String realNameCache = cacheService.getRealNameCache(cacheService.getRealNameCacheMap(), JwtUtil.getUserForToken());
        sapLuckyDrawUser.setLuckyDrawName(realNameCache);
        Integer luckyDrawThemeLast = otherMapper.getLuckyDrawThemeLast();
        sapLuckyDrawUser.setLuckyDrawThemeId(luckyDrawThemeLast);
        String luckyDrawThemeContext = otherMapper.getLuckyDrawThemeById(otherMapper.getLuckyDrawThemeLast()).getLuckyDrawThemeContext();
        sapLuckyDrawUser.setLuckyDrawTheme(luckyDrawThemeContext);
        sapLuckyDrawUser.setLuckyDrawTime(new Timestamp(System.currentTimeMillis()));
        otherMapper.joinLuckyDrawByUsername(sapLuckyDrawUser);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<SapLuckyDrawUser> randomLuckyDraws(Long luckyDrawThemePriceId) {
        List<SapLuckyDrawUser> luckyDrawUserById = otherMapper.getLuckyDrawUserById(otherMapper.getLuckyDrawThemeLast());
        if (luckyDrawUserById.isEmpty()) {
            return null;
        }
        Random random = new Random();
        int people = random.nextInt(luckyDrawUserById.size());

        SapLuckyDrawThemePrice luckDrawPriceLastByPriceId = otherMapper.getLuckDrawPriceLastByPriceId(luckyDrawThemePriceId);
        if (luckDrawPriceLastByPriceId == null) {
            return null;
        }
        String username = luckyDrawUserById.get(people).getLuckyDrawUsername();
        String name = luckyDrawUserById.get(people).getLuckyDrawName();
        otherMapper.updateLuckyDrawUserByUsername(
                new SapLuckyDrawUser(username,
                luckDrawPriceLastByPriceId.getLuckyDrawThemePrice(),
                luckDrawPriceLastByPriceId.getLuckyDrawThemePriceImage(),
                new Timestamp(System.currentTimeMillis())));

        otherMapper.updateLuckDrawPriceNums(luckyDrawThemePriceId,
                luckDrawPriceLastByPriceId.getLuckyDrawThemePriceNums() - 1);
        SapLuckyDrawTheme luckyDrawThemeById = otherMapper.getLuckyDrawThemeById(otherMapper.getLuckyDrawThemeLast());
        String email = EmailUntil.emailForPriceDraw(name,luckyDrawThemeById.getLuckyDrawThemeContext(),luckDrawPriceLastByPriceId.getLuckyDrawThemePrice());
        iMailService.sendHtmlMail(userService.getUserMessage(username).getQq()+"@qq.com","软件协会获奖通知",email);
        return otherMapper.getAwardInformationById(otherMapper.getLuckyDrawThemeLast());
    }

    @Override
    public List<SapLuckyDrawUser> getAwardInformation() {
        return otherMapper.getAwardInformationById(otherMapper.getLuckyDrawThemeLast());
    }

    @Override
    public SapLuckyDrawUser isJoinLuckyDraw() {
        return otherMapper.isJoinLuckyDraw(otherMapper.getLuckyDrawThemeLast(), JwtUtil.getUserForToken());
    }
}
